//
//  AutoButtonTableView.m
//  RightSlide
//
//  Created by Pill Gong on 13-12-13.
//  Copyright (c) 2013年 Pill Gong. All rights reserved.
//

#import "AutoButtonTableView.h"

@interface AutoButtonTableView ()

@property (nonatomic, retain) UIScrollView *contentView;
@property (nonatomic, assign) CGFloat viewWidth;

@end

@implementation AutoButtonTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
        self.contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:_contentView];
        _viewWidth = frame.size.width;
        [self setDefaultValues];
    }
    return self;
}

//- (void)dealloc {
//    [_contentView release];
//    [super dealloc];
//}

- (void)setDefaultValues {
    _titleFontSize = 12;
    _eachCellButtonNumber = 3;
    _paddingWidth  = 10;
    _paddingHeight = 10;
    _buttonHeight  = 35;
}

- (void)setButtonTextArray:(NSArray *)buttonTextArray {
//    [_buttonTextArray release];
    _buttonTextArray = buttonTextArray;
}

- (void)show {
    [self createButtons];
    [self updateContentSize];
}

- (void)updateContentSize {

}

- (void)createButtons {
    
    CGFloat buttonWidth = [self getEachButtonWidth];
    for (int i=0; i < _buttonTextArray.count; i++) {
        CGFloat buttonX = _paddingWidth + (buttonWidth + _paddingWidth) * (i%_eachCellButtonNumber);
        CGFloat buttonY = _paddingHeight + (_buttonHeight + _paddingHeight) * (i/3);
        
        UIButton *btn = [self getButtonItemWithText:[_buttonTextArray objectAtIndex:i] tag:i];
        btn.frame = CGRectMake(buttonX, buttonY, buttonWidth, _buttonHeight);
        [_contentView addSubview:btn];
    }

    int cellNumber = (_buttonTextArray.count - 1) / _eachCellButtonNumber + 1;
    
    self.contentView.contentSize = CGSizeMake(self.contentView.contentSize.width, (_paddingHeight + (_buttonHeight + _paddingHeight)*cellNumber+50));
}

- (UIButton *)getButtonItemWithText:(NSString *)text tag:(int)tag {
//    NSLog(@"%i",_titleFontSize);
    UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    if((tag/3)%2==0){
        [tempBtn setBackgroundImage:[UIImage imageNamed:@"img_shhk_bmfw_item_bg"] forState:UIControlStateNormal];
    }else{
        [tempBtn setBackgroundImage:[UIImage imageNamed:@"img_shhk_bmfw_item_bg"] forState:UIControlStateNormal];
    }
    [tempBtn setBackgroundImage:[UIImage imageNamed:@"img_shhk_bmfw_item_bg_on"] forState:UIControlStateHighlighted];
    
    [tempBtn setTitle:text forState:UIControlStateNormal];
    [tempBtn setTitle:text forState:UIControlStateHighlighted];
    
    [tempBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:_titleFontSize]];
    
    [tempBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [tempBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    tempBtn.tag = tag;
    tempBtn.titleLabel.lineBreakMode = 0;//自动换行
    tempBtn.titleLabel.textAlignment = NSTextAlignmentCenter;//居中显示
    [tempBtn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    return tempBtn;
}

- (CGFloat)getEachButtonWidth {
    return (_viewWidth - _paddingWidth * (_eachCellButtonNumber + 1)) / _eachCellButtonNumber;
}

- (void)buttonPressed:(UIButton *)btn {
    if (_delegate && [_delegate respondsToSelector:@selector(buttonPressed:buttonTag:)]) {
        [_delegate buttonPressed:[_buttonTextArray objectAtIndex:btn.tag] buttonTag:btn.tag];
    }
}

@end
